﻿using System;
using Company.BLL;
using Company.BLL.KDT.SXXK;
using Company.BLL.DuLieuChuan;
using Company.Interface.SXXK;
using NguyenPhuLieu=Company.BLL.SXXK.NguyenPhuLieu;
using SanPham=Company.BLL.SXXK.SanPham;

namespace Company.Interface.KDT.SXXK
{
    public partial class DinhMucEditForm : BaseForm
    {
        public DinhMuc DMDetail = new DinhMuc();
        public DinhMucCollection DMCollection;
        public DinhMucDangKy dmDangKy;
        private bool existNPL;
        private bool existSP;
        public string maSP = "";
        SelectSanPhamDMForm fsp = new SelectSanPhamDMForm();
        SelectNguyenPhuLieuDMForm fnpl = new SelectNguyenPhuLieuDMForm();
        public DinhMucEditForm()
        {
            InitializeComponent();
            fsp.status = true;
            fnpl.status = true;
        }

        private void txtMaNPL_Leave(object sender, EventArgs e)
        {
            NguyenPhuLieu nplSXXK = new NguyenPhuLieu();
            nplSXXK.MaHaiQuan = this.MaHaiQuan;
            nplSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            nplSXXK.Ma = txtMaNPL.Text;
            if (nplSXXK.Load())
            {
                this.existNPL = true;
                txtMaNPL.Text = nplSXXK.Ma;
                txtTenNPL.Text = nplSXXK.Ten;
                txtMaHSNPL.Text = nplSXXK.MaHS;
                txtDonViTinhNPL.Text = DonViTinh.GetName(nplSXXK.DVT_ID);
                error.SetError(txtMaNPL, string.Empty);
            }
            else
            {
                this.existNPL = false;
                error.SetIconPadding(txtMaNPL, -8);
                error.SetError(txtMaNPL, "Không tồn tại nguyên phụ liệu này.");
                
                txtTenNPL.Text = txtMaHSNPL.Text = txtDonViTinhNPL.Text = string.Empty;
            }
        }

        private void txtMaSP_ButtonClick(object sender, EventArgs e)
        {
            if (fsp == null)
            {
                fsp = new SelectSanPhamDMForm();
                fsp.status = true;
            }
            fsp.CalledForm = this.Name;
            fsp.ShowDialog();            
            
            if (fsp.SanPhamSelected.Ma.Length > 0)
            {
                existSP = true;
                txtMaSP.Text = fsp.SanPhamSelected.Ma;
                txtTenSP.Text = fsp.SanPhamSelected.Ten;
                txtMaHSSP.Text = fsp.SanPhamSelected.MaHS;
                txtDonViTinhSP.Text = DonViTinh.GetName(fsp.SanPhamSelected.DVT_ID);
            }
        }

        private void txtMaNPL_ButtonClick(object sender, EventArgs e)
        {
            if (fnpl == null)
            {
                fnpl = new SelectNguyenPhuLieuDMForm();
                fnpl.status = true;
            }
            fnpl.CalledForm = this.Name;
            fnpl.ShowDialog(this);
            if (fnpl.NguyenPhuLieuSelected.Ma.Length > 0)
            {
                existNPL = true;
                txtMaNPL.Text = fnpl.NguyenPhuLieuSelected.Ma;
                txtTenNPL.Text = fnpl.NguyenPhuLieuSelected.Ten;
                txtMaHSNPL.Text = fnpl.NguyenPhuLieuSelected.MaHS;
                txtDonViTinhNPL.Text = DonViTinh.GetName(fnpl.NguyenPhuLieuSelected.DVT_ID);
            }
        }
        private bool CheckDMExit(string maSP, string maNPL)
        {
            if (DinhMuc.checkDinhMucExit(maSP, maNPL, dmDangKy.ID))
            {
                ShowMessage("Định mức này đã được khai báo trong danh sách định mức khác.", false);
                return true;
            }
            foreach(DinhMuc dm in DMCollection)
            {
                if (dm.MaSanPham == maSP && dm.MaNguyenPhuLieu == maNPL)
                {
                    ShowMessage("Định mức này đã được khai báo trên lưới.", false);                    
                    return true;
                }
            }
            return false;
        }
        private void DinhMucEditForm_Load(object sender, EventArgs e)
        {
            if (this.OpenType == OpenFormType.Edit)
            {
                SanPham spSXXK = new SanPham();
                spSXXK.MaHaiQuan = this.MaHaiQuan;
                spSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                spSXXK.Ma = DMDetail.MaSanPham;
                if (spSXXK.Load())
                {
                    this.existSP = true;
                    txtMaSP.Text = spSXXK.Ma;
                    txtTenSP.Text = spSXXK.Ten;
                    txtMaHSSP.Text = spSXXK.MaHS;
                    txtDonViTinhSP.Text = DonViTinh.GetName(spSXXK.DVT_ID);
                }
                else
                {
                    Company.BLL.KDT.SXXK.SanPham spKDT = Company.BLL.KDT.SXXK.SanPham.Load(DMDetail.MaSanPham);
                    this.existSP = true;
                    txtMaSP.Text = spKDT.Ma;
                    txtTenSP.Text = spKDT.Ten;
                    txtMaHSSP.Text = spKDT.MaHS;
                    txtDonViTinhSP.Text = DonViTinh.GetName(spKDT.DVT_ID);
                }
                NguyenPhuLieu nplSXXK = new NguyenPhuLieu();
                nplSXXK.MaHaiQuan = this.MaHaiQuan;
                nplSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
                nplSXXK.Ma = DMDetail.MaNguyenPhuLieu;
                if (nplSXXK.Load())
                {
                    this.existNPL = true;
                    txtMaNPL.Text = nplSXXK.Ma;
                    txtTenNPL.Text = nplSXXK.Ten;
                    txtMaHSNPL.Text = nplSXXK.MaHS;
                    txtDonViTinhNPL.Text = DonViTinh.GetName(nplSXXK.DVT_ID);
                }
                else
                {
                    Company.BLL.KDT.SXXK.NguyenPhuLieu nplKDT = Company.BLL.KDT.SXXK.NguyenPhuLieu.Load(DMDetail.MaSanPham);
                    this.existNPL = true;
                    txtMaNPL.Text = nplKDT.Ma;
                    txtTenNPL.Text = nplKDT.Ten;
                    txtMaHSNPL.Text = nplKDT.MaHS;
                    txtDonViTinhNPL.Text = DonViTinh.GetName(nplKDT.DVT_ID);
                }
                txtDinhMuc.Value = DMDetail.DinhMucSuDung;
                txtTyLeHH.Value = DMDetail.TyLeHaoHut;

            }
            else
            {
                txtMaSP.Text = this.maSP;
            }
        }

        private void txtMaSP_Leave(object sender, EventArgs e)
        {
            SanPham spSXXK = new SanPham();
            spSXXK.MaHaiQuan = this.MaHaiQuan;
            spSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            spSXXK.Ma = txtMaSP.Text;
            if (spSXXK.Load())
            {
                this.existSP = true;
                error.SetError(txtMaNPL, string.Empty);

                txtMaSP.Text = spSXXK.Ma;
                txtTenSP.Text = spSXXK.Ten;
                txtMaHSSP.Text = spSXXK.MaHS;
                txtDonViTinhSP.Text = DonViTinh.GetName(spSXXK.DVT_ID);
                error.SetError(txtMaSP, string.Empty);
            }
            else
            {
                this.existSP = false;
                error.SetIconPadding(txtMaSP, -8);
                error.SetError(txtMaSP, "Không tồn tại sản phẩm này.");

                txtTenSP.Text = txtMaHSSP.Text = txtDonViTinhSP.Text = string.Empty;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            // 1. Kiểm tra dữ liệu.
            cvError.Validate();
            if (!cvError.IsValid) return;

            if (!this.existNPL)
            {
                error.SetIconPadding(txtMaNPL, -8);
                error.SetError(txtMaNPL, "Không tồn tại nguyên phụ liệu này.");
                return;
            }
            if (!this.existSP)
            {
                error.SetIconPadding(txtMaSP, -8);
                error.SetError(txtMaSP, "Không tồn tại sản phẩm này.");
                return;
            }
            if (Convert.ToDecimal(txtDinhMuc.Text)<=0)
            {
                error.SetIconPadding(txtDinhMuc, -8);
                error.SetError(txtDinhMuc, "Định mức phải lớn hơn 0.");
                return;
            }
            if (CheckDMExit(txtMaSP.Text, txtMaNPL.Text))
            {
                return;
            }
            //kiem tra xem da dang ky dinh muc chua
            Company.BLL.SXXK.ThongTinDinhMuc dm = new Company.BLL.SXXK.ThongTinDinhMuc();
            dm.MaHaiQuan = dmDangKy.MaHaiQuan;
            dm.MaDoanhNghiep = GlobalSettings.MA_DON_VI;            
            dm.MaSanPham = txtMaSP.Text.Trim();
            if (dm.Load())
            {
                ShowMessage("Sản phẩm này đã được đăng ký.", false);
                return;
            }

            if (this.OpenType == OpenFormType.Insert)
            {
                // 2. Cập nhật dữ liệu.
                this.DMDetail.MaSanPham = txtMaSP.Text;
                this.DMDetail.MaNguyenPhuLieu = txtMaNPL.Text;
                this.DMDetail.DinhMucSuDung = Convert.ToDecimal(txtDinhMuc.Value);
                this.DMDetail.TyLeHaoHut = Convert.ToDecimal(txtTyLeHH.Value);
                this.DMDetail.DVT_ID = this.DonViTinh_GetID(txtDonViTinhNPL.Text.Trim());
            }
            if (this.OpenType == OpenFormType.Edit)
            {
                this.DMDetail.MaSanPham = txtMaSP.Text;
                this.DMDetail.MaNguyenPhuLieu = txtMaNPL.Text;
                this.DMDetail.DinhMucSuDung = Convert.ToDecimal(txtDinhMuc.Value);
                this.DMDetail.TyLeHaoHut = Convert.ToDecimal(txtTyLeHH.Value);
                this.DMDetail.DVT_ID = this.DonViTinh_GetID(txtDonViTinhNPL.Text.Trim());
                if (this.DMDetail.ID > 0) 
                {
                    this.DMDetail.Update();
                }
            }
            // 3. Đóng.
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if(this.OpenType==OpenFormType.Insert)
                this.DMDetail = null;
            this.Close();
        }

    }
}